#!/usr/bin/env bash

############## author      : zhanpu
############## date        : 2019-06-21
############## description : 服务启动脚本，无需要运维修改

#返回值
RETVAL=$?

readonly SERVER_HOME=$(cd $(dirname $0); pwd)
readonly SERVER_NAME="datax-admin"
readonly SERVER_VERSION="2.1.0"
readonly SERVER_LOGS="${SERVER_HOME}/logs"
#进程id
readonly PID_FILE="${SERVER_HOME}/conf/app.pid"
#日志文件
readonly LOG_FILE="${SERVER_HOME}/logs/${SERVER_NAME}.log"

#创建日志目录
if [ -e !"${SERVER_LOGS}" ]; then
    echo $"logs directory is exists ..."
else
    mkdir -p "${SERVER_LOGS}"
fi

#定义jvm参数
JVM_OPTION="-server -Xms6g -Xmx6g -Xmn4500m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=64m -XX:SurvivorRatio=4 -XX:MaxTenuringThreshold=5 \
            -XX:GCTimeRatio=19 -Xnoclassgc -XX:+DisableExplicitGC -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection \
            -XX:CMSFullGCsBeforeCompaction=0 -XX:-CMSParallelRemarkEnabled -XX:CMSInitiatingOccupancyFraction=70 -XX:SoftRefLRUPolicyMSPerMB=0 \
            -XX:+PrintClassHistogram -XX:+PrintGCDetails -XX:+DisableExplicitGC -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -Xloggc:${SERVER_HOME}/logs/gc.log \
            -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${SERVER_HOME}/logs -Dfile.encoding=UTF-8 -Dserver.home=${SERVER_HOME} -Dserver.version=${SERVER_VERSION} -Dserver.name=${SERVER_NAME}"


JRE_EXISTS=`which java | grep -v grep | awk '{ print $1 }'`
#定义启动命令
START_COMMAND=

if [ -e "${JRE_EXISTS}" ]; then
    SERVER_JAR="$SERVER_NAME-$SERVER_VERSION.jar"
    START_COMMAND="nohup java ${JVM_OPTION} -jar $SERVER_JAR"
else
    echo $"jre not exists ..."
    exit 1
fi

#检查pid（检查服务是否启动）
function check_pid() {
    if [ -f "${PID_FILE}" ]; then
        PID=`cat ${PID_FILE}`
        if [ -n "${PID}" ]; then
            RUNNING=`ps -p "${PID}"|grep -v "PID TTY" |wc -l`
            return ${RUNNING}
        fi
    fi
    return 0
}

#启动服务
function start() {
    check_pid
    RUNNING=$?
    if [ ${RUNNING} -gt 0 ]; then
        echo -n $"${SERVER_NAME} now is running already, pid="
        cat ${PID_FILE}
        return 1
    fi

    echo $"Started ${SERVER_NAME} service ..."
    #启动命令并重定向
    ${START_COMMAND}>/dev/null 2>&1 &
    echo $! > "${PID_FILE}"
    echo $"Started ${SERVER_NAME} service successfully ..., pid=$!"
}

#关闭服务
function stop() {
    PID=`cat ${PID_FILE}`
    kill -15 "${PID}"
    echo $"Stoped ${SERVER_NAME} service successfully ..."
}

#重启服务
function restart() {
    stop
    sleep 1
    start
}

#查看服务状态
function status() {
    check_pid
    RUNNING=$?
    if [ ${RUNNING} -gt 0 ]; then
        echo -n $"${SERVER_NAME} now is running, pid="
        cat ${PID_FILE}
    else
        echo $"${SERVER_NAME} is stoped ..."
    fi
}

#查看日志
function tailf() {
    tail -f "${LOG_FILE}"
}

#查看帮助
function help() {
    echo $"Usage: $0 {start|stop|restart|status|tailf}"
}


case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    restart
    ;;
status)
    status
    ;;
tailf)
    tailf
    ;;
*)
    help
    exit 1
    ;;
esac

exit $RETVAL